// //@ts-check
import js from "@eslint/js";
import vitest from "@vitest/eslint-plugin";
import eslintConfigPrettier from "eslint-config-prettier";
import playwright from "eslint-plugin-playwright";
import eslintPluginSvelte from "eslint-plugin-svelte";
import globals from "globals";
import tsEslint from "typescript-eslint";
import { globalIgnores } from "eslint/config";

export default [
  js.configs.recommended,
  ...tsEslint.configs.recommended,
  ...eslintPluginSvelte.configs["flat/recommended"],
  eslintConfigPrettier,
  {
    files: ["**/tests/**"],
    ...playwright.configs["flat/playwright"],
    ...vitest.configs.recommended,
  },
  ...eslintPluginSvelte.configs["flat/prettier"],
  {
    languageOptions: {
      ecmaVersion: "latest",
      sourceType: "module",
      globals: { ...globals.node, ...globals.browser },
      parserOptions: {
        project: true,
        tsconfigRootDir: import.meta.dirname,
        parser: tsEslint.parser,
        extraFileExtensions: [".svelte"],
      },
    },
    rules: {
      "@typescript-eslint/no-explicit-any": "warn",
      "@typescript-eslint/no-unused-vars": [
        "error",
        { argsIgnorePattern: "^_" },
      ],
      "@typescript-eslint/ban-ts-comment": "warn",
      "@typescript-eslint/no-unsafe-assignment": "warn",
      "@typescript-eslint/no-unsafe-member-access": "warn",
      "@typescript-eslint/no-unsafe-argument": "warn",
      "@typescript-eslint/no-unsafe-call": "warn",
      "@typescript-eslint/no-unsafe-return": "warn",
      "@typescript-eslint/no-floating-promises": "warn",
      "@typescript-eslint/no-unnecessary-type-assertion": "warn",
      "@typescript-eslint/unbound-method": "warn",
      "@typescript-eslint/require-await": "warn",
      "@typescript-eslint/restrict-template-expressions": "warn",
      "@typescript-eslint/no-redundant-type-constituents": "off",
      "@typescript-eslint/no-unsafe-enum-comparison": "warn",
      "@typescript-eslint/no-misused-promises": "warn",
      "@typescript-eslint/no-duplicate-enum-values": "warn",
      "@typescript-eslint/await-thenable": "warn",
      "@typescript-eslint/no-implied-eval": "warn",
      "@typescript-eslint/no-base-to-string": "warn",
      "@typescript-eslint/no-unused-expressions": "warn",
      "@typescript-eslint/no-require-imports": "warn",
    },
  },
  {
    ignores: [
      "**/.storybook/*",
      "**/.svelte-kit/",
      "**/gen/*",
      "**/node_modules",
      "**/playwright.config.js",
      "**/postcss.config.cjs",
      "**/svelte.config.js",
      "web-admin/build/*",
      "web-admin/playwright-report/*",
      "web-admin/playwright/*",
      "web-common/build/*",
      "web-common/src/components/modal/*.js",
      "web-common/src/features/dashboards/url-state/filters/expression.cjs", // generated by nearly
      "web-local/build/*",
    ],
  },
  {
    files: ["*.js"],
    ...tsEslint.configs.disableTypeChecked,
  },
  {
    files: ["**/*.svelte"],
    rules: {
      // ERRORS
      "svelte/no-at-debug-tags": "error",
      "svelte/valid-prop-names-in-kit-pages": "error",
      // WARNINGS (some will be promoted to errors)
      "svelte/no-at-html-tags": "warn",
      "svelte/no-target-blank": "warn",
      "svelte/no-unused-svelte-ignore": "warn",
      "svelte/require-each-key": "warn",
      "svelte/prefer-destructured-store-props": "warn",
      "svelte/require-optimized-style-attribute": "warn",
      "svelte/prefer-class-directive": "warn",
      "svelte/require-store-reactive-access": "warn",
      //   "svelte/require-event-dispatcher-types": "warn",
      //   "svelte/sort-attributes": "warn",
    },
  },
  {
    rules: {
      "svelte/no-inner-declarations": "off", // This rule isn't working
    },
  },
  globalIgnores([
    "*/src/features/dashboards/url-state/time-ranges/rill-time.cjs",
  ]),
];
